package com.bdzl.admin.service.data.impl;

import com.bdzl.admin.service.data.EnterpriseHolderService;
import com.bdzl.pub.entity.data.EnterpriseHolderDO;
import com.bdzl.pub.util.file.BASE64DecodedMultipartFile;
import com.bdzl.pub.util.file.OssUploadFileUtil;
import com.bdzl.pub.util.param.R;
import com.bdzl.pub.vo.EnterpriseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.bdzl.admin.dao.data.EnterpriseDao;
import com.bdzl.pub.entity.data.EnterpriseDO;
import com.bdzl.admin.service.data.EnterpriseService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;


@Service
@Transactional(rollbackFor = Exception.class)
public class EnterpriseServiceImpl implements EnterpriseService {
	@Autowired
	private EnterpriseDao enterpriseDao;
	@Autowired
	private EnterpriseService enterpriseService;
	@Autowired
	private EnterpriseHolderService enterpriseHolderService;
	
	@Override
	public EnterpriseDO get(Integer entId){
		return enterpriseDao.get(entId);
	}
	
	@Override
	public List<EnterpriseDO> list(Map<String, Object> map){
		return enterpriseDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return enterpriseDao.count(map);
	}
	
	@Override
	public int save(EnterpriseDO enterprise){
		try {
			if (enterprise.getEntLicensePic()!=null && !enterprise.getEntLicensePic().startsWith("http")) {
				//上传百度云
				MultipartFile file1 = BASE64DecodedMultipartFile.base64ToMultipart(enterprise.getEntLicensePic());
				String docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file1, "loan/ent/ent_license");
				//修改头像
				enterprise.setEntLicensePic(docUrl);
			}
			if (enterprise.getEntSealPic()!=null && !enterprise.getEntSealPic().startsWith("http")) {
				//上传百度云
				MultipartFile file1 = BASE64DecodedMultipartFile.base64ToMultipart(enterprise.getEntSealPic());
				String docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file1, "loan/ent/ent_seal  ");
				//修改头像
				enterprise.setEntSealPic(docUrl);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return enterpriseDao.save(enterprise);
	}
	
	@Override
	public int update(EnterpriseDO enterprise){
		try {
			if (enterprise.getEntLicensePic()!=null && !enterprise.getEntLicensePic().startsWith("http")) {
				//上传百度云
				MultipartFile file1 = BASE64DecodedMultipartFile.base64ToMultipart(enterprise.getEntLicensePic());
				String docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file1, "loan/ent/ent_license");
				//修改头像
				enterprise.setEntLicensePic(docUrl);
				//获取旧头像
				String userHeadImg = enterpriseDao.get(enterprise.getEntId()).getEntLicensePic();
				if (userHeadImg != null && userHeadImg.startsWith("http")) {
					OssUploadFileUtil.remove(userHeadImg);
				}
			}
			if (enterprise.getEntSealPic()!=null && !enterprise.getEntSealPic().startsWith("http")) {
				//上传百度云
				MultipartFile file1 = BASE64DecodedMultipartFile.base64ToMultipart(enterprise.getEntSealPic());
				String docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file1, "loan/ent/ent_seal");
				//修改头像
				enterprise.setEntSealPic(docUrl);
				//获取旧头像
				String userHeadImg = enterpriseDao.get(enterprise.getEntId()).getEntSealPic();
				if (userHeadImg != null && userHeadImg.startsWith("http")) {
					OssUploadFileUtil.remove(userHeadImg);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return enterpriseDao.update(enterprise);
	}
	
	@Override
	public int remove(Integer entId){
		return enterpriseDao.remove(entId);
	}
	
	@Override
	public int batchRemove(Integer[] entIds){
		return enterpriseDao.batchRemove(entIds);
	}

	@Override
	public List<EnterpriseVO> enterpriseAndHolder(Map map) {
		return enterpriseDao.enterpriseAndHolder(map);
	}

	@Override
	public int enterpriseAndHoldercount(Map map) {
		return enterpriseDao.enterpriseAndHoldercount(map);
	}

	@Override
	public R updateEnterpriseAndHolder(EnterpriseVO enterpriseVO, Integer userId) {

		List<EnterpriseHolderDO> enterpriseHolderDOList = enterpriseVO.getEnterpriseHolderDOList();
		if (enterpriseVO.getEntLicensePic() != null && enterpriseVO.getEntLicensePic().indexOf("http") != 0) {
			String docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(enterpriseVO.getEntLicensePic(), "loan/ent/ent_license/" + enterpriseVO.getEntId() + "license");
			enterpriseVO.setEntLicensePic(docUrl);
		}
		if (enterpriseVO.getEntSealPic() != null && enterpriseVO.getEntSealPic().indexOf("http") != 0) {
			String docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(enterpriseVO.getEntSealPic(), "loan/ent/ent_seal/" + enterpriseVO.getEntId() + "seal");
			enterpriseVO.setEntSealPic(docUrl);
		}
		//1.修改 企业信息
		int a = enterpriseService.update(enterpriseVO);
		//2. 修改股东信息
		for (EnterpriseHolderDO enterpriseHolderDO : enterpriseHolderDOList) {
			if (enterpriseHolderDO.getHolderId() != null) {
				//2.1 修改股东信息
				enterpriseHolderDO.setHolderModuser(userId);
				enterpriseHolderDO.setHoderEntNo(enterpriseVO.getEntNo());
				enterpriseHolderService.update(enterpriseHolderDO);
			} else {
				//2.2 添加股东信息
				enterpriseHolderDO.setHoderEntNo(enterpriseVO.getEntNo());
				enterpriseHolderService.save(enterpriseHolderDO);
			}
		}
		return R.ok();
	}
}
